<template>
  <div>
    <el-card>
      <!-- sku详情 -->
      <el-table border :data="skuList" style="width: 100%">
        <el-table-column align="center" type="index" label="序号" width="80">
        </el-table-column>

        <el-table-column prop="skuName" label="名称" width="width">
        </el-table-column>

        <el-table-column prop="skuDesc" label="描述" width="width">
        </el-table-column>

        <el-table-column prop="prop" label="默认图片" width="150">
          <template slot-scope="{ row }">
            <div class="info">
              <div class="pic">
                <img :src="row.skuDefaultImg" style="width: 80px" alt="" />
              </div>
            </div>
          </template>
        </el-table-column>

        <el-table-column prop="weight" label="重量(KG)" width="width">
        </el-table-column>

        <el-table-column prop="price" label="价格(元)" width="80">
        </el-table-column>

        <el-table-column prop="prop" label="操作" width="250">
          <template slot-scope="{ row }">
            <!-- 上架 -->
            <HintButton
              v-if="row.isSale == 0"
              size="mini"
              title="上架"
              type="success"
              icon="el-icon-top"
              @click="onSale(row.id)"
            >
            </HintButton>
            <!-- 下架 -->
            <HintButton
              v-if="row.isSale == 1"
              size="mini"
              title="下架"
              type="info"
              icon="el-icon-bottom"
              @click="cancelSale(row.id)"
            >
            </HintButton>
            <HintButton
              size="mini"
              type="primary"
              titlt="修改"
              icon="el-icon-edit"
            >
            </HintButton>
            <HintButton
              size="mini"
              type="info"
              icon="el-icon-info"
              titlt="查看详情"
              @click="showSkuInfo(row.id)"
            ></HintButton>
            <HintButton
              size="mini"
              type="danger"
              titlt="删除"
              icon="el-icon-delete"
              @click="remove(row.id)"
            >
            </HintButton>
          </template>
        </el-table-column>
      </el-table>

      <!-- 分页器 -->
      <el-pagination
        @size-change="changeSize"
        @current-change="getSkuList"
        style="text-align: center"
        :current-page="page"
        :page-sizes="[10, 15, 20]"
        :page-size="limit"
        layout="prev, pager, next, jumper,->,sizes,total"
        :total="total"
      >
      </el-pagination>

      <!-- 侧边栏 -->
      <el-drawer
        class="sku-list"
        :visible.sync="isShowSkuInfo"
        :with-header="false"
        size="50%"
      >
        <el-row>
          <el-col :span="5">名称</el-col>
          <el-col :span="16">{{ skuInfo.skuName }}</el-col>
        </el-row>
        <el-row>
          <el-col :span="5">描述</el-col>
          <el-col :span="16">{{ skuInfo.skuDesc }}</el-col>
        </el-row>
        <el-row>
          <el-col :span="5">价格</el-col>
          <el-col :span="16">{{ skuInfo.price }}元</el-col>
        </el-row>
        <el-row>
          <el-col :span="5">平台属性</el-col>
          <el-col :span="16">
            <el-tag
              type="success"
              v-for="value in skuInfo.skuAttrValueList"
              :key="value.id"
            >
              {{ value.attrId + "-" + value.valueId }}
            </el-tag>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="5">商品图片</el-col>
          <el-col :span="16">
            <el-carousel class="sku-carousel">
              <el-carousel-item v-for="imgList in skuInfo.skuImageList" :key="imgList.id">
               <img :src="imgList.imgUrl" alt="">
              </el-carousel-item>
            </el-carousel>
          </el-col>
        </el-row>
      </el-drawer>
    </el-card>
  </div>
</template>

<script>
export default {
  name: "SkuList",
  data() {
    return {
      page: 1,
      limit: 10,
      total: 0,
      skuList: [],
      loading: false,

      skuInfo: {},
      isShowSkuInfo: false,
    };
  },
  mounted() {
    this.getSkuList();
  },
  methods: {
    async getSkuList(page = 1) {
      this.page = page;
      const result = await this.$API.sku.getList(this.page, this.limit);
      if (result.code === 200 || result.code === 20000) {
        this.skuList = result.data.records;
        this.total = result.data.total;
        this.loading = false;
      }
    },

    // 页码发生改变自动调整
    changeSize(size) {
      this.limit = size;
      this.getSkuList();
    },

    // 上架
    async onSale(skuId) {
      try {
        const result = await this.$API.sku.onSale(skuId);
        if (result.code === 200 || result.code === 20000) {
          this.$message.success("上架成功");
          this.getSkuList(this.page);
        }
      } catch (error) {
        this.$$message.error("上架失败");
      }
    },

    // 下架
    async cancelSale(skuId) {
      try {
        const result = await this.$API.sku.cancelSale(skuId);
        if (result.code === 200 || result.code === 20000) {
          this.$message.success("下架成功");
          this.getSkuList(this.page);
        }
      } catch (error) {
        this.$message.error("下架失败");
      }
    },

    // 删除
    async remove(skuId) {
      try {
        const result = await this.$API.sku.remove(skuId);
        if (result.code === 20000 || result.code === 200) {
          this.$message.success("删除成功");
          this.getSkuList(1);
        }
      } catch (error) {
        this.$message.error("删除失败");
      }
    },

    // 详细信息
    async showSkuInfo(id) {
      this.isShowSkuInfo = true;
      const result = await this.$API.sku.get(id);
      this.skuInfo = result.data;
    },
  },
};
</script>

<style lang="scss" scoped>
.sku-list {
  .el-row {
    height: 40px;
    .el-col {
      line-height: 40px;
      &.el-col-5 {
        font-size: 18px;
        font-weight: bold;
        text-align: right;
        margin-right: 20px;
      }
    }
  }

  .sku-carousel {
    width: 400px;
    border: 1px solid #ccc;
    box-sizing: border-box;
    img {
      width: 400px;
      height: 400px;
    }
  }

  // 深度作用选择器
  ::v-deep .el-carousel__indicator {
    button {
      display: inline-block;
      width: 30px;
      height: 30px;
      border-radius: 50%;
      background-color: hotpink;
    }
    &.is-active {
      button {
        background-color: purple;
      }
    }
  }
}
</style>
